from PyQt5.QtCore import QThread, pyqtSignal
import vlc

class play_control_thread (QThread):
    
    statusSignal = pyqtSignal(str)
    
    def __init__(self):
        super(play_control_thread, self).__init__()
        self.instance = vlc.Instance()
        self.player = self.instance.media_player_new()
        
    def set_property(self, basestr, volume):
        self.basestr = basestr
        self.volume = volume
    
    def run(self):
        print("get %s successfully, current volume is %d" % (self.basestr, self.volume))
            
        self.media = self.instance.media_new(self.basestr)
        self.player.set_media(self.media)
        self.media.parse_with_options(vlc.MediaParseFlag.network,5000)
        
        while True:
            if self.media.get_parsed_status() == vlc.MediaParsedStatus.done:
                self.player.audio_set_volume(self.volume)
        
                self.player.play()
                self.player.set_pause(1)
                
                self.statusSignal.emit('normal')
                break
            elif self.media.get_parsed_status() == vlc.MediaParsedStatus.timeout:
                print('timeout got in thread')
                self.statusSignal.emit('abnormal')
                break
            elif self.media.get_parsed_status() == vlc.MediaParsedStatus.failed:
                self.statusSignal.emit('abnormal')
                break
            
    def get_player(self):
        return self.player
    
    def get_instance(self):
        return self.instance
        
    def get_media(self):
        return self.media

